<?php
/**
 * @Name 查看表字段
 * @Description
 * @Auther 宋博
 * @Date 2021/5/18 11:20
 */

namespace Modules\SqlQeneratorApi\Http\service;


use Illuminate\Support\Facades\DB;
use Modules\Common\Services\CommonServices;
class ViewTableFieldService extends CommonServices
{
    /**
     * @Name  查看表字段
     * @Description
     * @Author 宋博
     * @Date 2021/5/18 11:27
     * @Param data.tableName  String  表名
     * @Param data.tableComment  String  表备注
     * @Return Json
     */
    public function index($data){
        $table = DB::select("select * from information_schema.COLUMNS where table_name='".$data['tableName']."'");
        $arr = [];
        //便利表
        foreach ($table as $k => $v){
            //调用PHP格式化字节大小函数
            $arr[$k]['COLUMN_NAME'] = $v->COLUMN_NAME;//字段名
            $arr[$k]['COLUMN_TYPE'] = $v->COLUMN_TYPE;//数据类型
            $arr[$k]['COLUMN_DEFAULT'] = $v->COLUMN_DEFAULT;//默认值
            $arr[$k]['IS_NULLABLE'] = $v->IS_NULLABLE;//允许非空
            $arr[$k]['EXTRA'] = ($v->EXTRA == 'auto_increment'?'是':' ');//自动递增
            $arr[$k]['COLUMN_COMMENT'] = $v->COLUMN_COMMENT;//备注
            $arr[$k]['table'] = $data;//表名
        }
        return $this->apiSuccess(['data'=>$arr]);
    }
}
